public class MyStack
{
	class Item
	{
		Item down;
		char d;

		Item(char _d)
		{
			d = _d;
		}
	}

	Item top;
	int size;

	MyStack()
	{
		super();
		top = null;
		size = 0;
	}

	public boolean isEmpty()
	{
		return (size == 0);
	}

	public void pop()
	{
		try
		{
			Item old = top;
			this.top = this.top.down;
			old.down = null; //so to finalize correctly
			size--;
		}
		catch (NullPointerException e)
		{
			e.printStackTrace();
		}
	}

	public void push(char toPush)
	{
		try
		{
			Item n = new Item(toPush);
			n.down = top;
			this.top = n;
			size++;
		}
		catch (NullPointerException e)
		{
			e.printStackTrace();
		}
	}

	public char pop2()
	{
		try
		{
			char res = top();
			pop();
			return res;
		}
		catch (NullPointerException e)
		{
			e.printStackTrace();
		}
		return '\0';
	}

	public char top() throws NullPointerException
	{
		return top.d;
	}

	protected void finalize() throws Throwable
	{
		// TODO Auto-generated method stub
		while (top != null)
		{
			pop();
		}
		super.finalize();
	}
}
